package monitor.mobie.hdy.im.utils;

import android.util.Log;

import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.RequestBody;

import okio.Buffer;

public class feb {
    private static final MediaType CONTENT_TYPE =
            MediaType.parse("application/json");

    private final Buffer content = new Buffer();

    private static final char[] HEX_DIGITS =
            { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
    static final String QUERY_COMPONENT_ENCODE_SET = " \"'<>#&=";


    static void canonicalize(Buffer out, String input, int pos, int limit,
                             String encodeSet, boolean alreadyEncoded, boolean query) {
        Buffer utf8Buffer = null; // Lazily allocated.
        int codePoint;
        for (int i = pos; i < limit; i += Character.charCount(codePoint)) {
            codePoint = input.codePointAt(i);
            if (alreadyEncoded
                    && (codePoint == '\t' || codePoint == '\n' || codePoint == '\f' || codePoint == '\r')) {
                // Skip this character.
            } else if (query && codePoint == '+') {
                // HTML permits space to be encoded as '+'. We use '%20' to avoid special cases.
                out.writeUtf8(alreadyEncoded ? "%20" : "%2B");
            } else if (codePoint < 0x20
                    || codePoint >= 0x7f
                    || encodeSet.indexOf(codePoint) != -1
                    || (codePoint == '%' && !alreadyEncoded)) {
                // Percent encode this character.
                if (utf8Buffer == null) {
                    utf8Buffer = new Buffer();
                }
                utf8Buffer.writeUtf8CodePoint(codePoint);
                while (!utf8Buffer.exhausted()) {
                    int b = utf8Buffer.readByte() & 0xff;
                    out.writeByte('%');
                    out.writeByte(HEX_DIGITS[(b >> 4) & 0xf]);
                    out.writeByte(HEX_DIGITS[b & 0xf]);
                }
            } else {
                // This character doesn't need encoding. Just copy it over.
                out.writeUtf8CodePoint(codePoint);
            }
        }
    }

    /** Add new key-value pair. */
    public feb add(String name, String value) {
        if (content.size() > 0) {
            content.writeByte(',');
        }
        canonicalize(content, name, 0, name.length(),
                QUERY_COMPONENT_ENCODE_SET, false, true);
        content.writeByte(':');
        canonicalize(content, value, 0, value.length(),
                QUERY_COMPONENT_ENCODE_SET, false, true);
        Log.i("content", content.toString());
        return this;
    }

    /** Add new key-value pair. */
    public feb addEncoded(String name, String value) {
        if (content.size() > 0) {
            content.writeByte('&');
        }
        canonicalize(content, name, 0, name.length(),
                QUERY_COMPONENT_ENCODE_SET, true, true);
        content.writeByte('=');
        canonicalize(content, value, 0, value.length(),
                QUERY_COMPONENT_ENCODE_SET, true, true);
        return this;
    }

    public RequestBody build() {
        if (content.size() == 0) {
            throw new IllegalStateException("Form encoded body must have at least one part.");
        }
        return RequestBody.create(CONTENT_TYPE, content.snapshot());
    }
}
